package com.crud.service.impl;

import com.crud.entity.Dept;
import com.crud.entity.DeptExample;
import com.crud.mapper.DeptMapper;
import com.crud.service.DeptService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author hyh
 * @date 2023/06/29 18:33:58
 */
@Service
@Transactional
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Override
    public PageInfo<Dept> listDept(Integer pageNum, Integer pageSize, String name) {
        DeptExample deptExample = null;
        if (name != null && !"".equals(name)){
            deptExample = new DeptExample();
            deptExample.or().andNameLike("%" + name + "%");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Dept> list = deptMapper.selectByExample(deptExample);
        PageInfo<Dept> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Integer save(Dept dept) {
        return deptMapper.insert(dept);
    }

    @Override
    public Integer update(Dept dept) {
        return deptMapper.updateByPrimaryKeySelective(dept);
    }

    @Override
    public Integer remove(Integer id) {
        return deptMapper.deleteByPrimaryKey(id);
    }
    @Override
    public List<Dept> findAll() {
        return deptMapper.selectByExample(null);
    }

}
